package trefliq.EJB.entities;

import java.io.Serializable;
import java.sql.Date;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.*;

/**
 *
 * @author Brandon
 * 
 */
@Entity
@NamedQueries ({
    @NamedQuery(name="findAllCarts", query="SELECT e FROM Cart e"),
    @NamedQuery(name="findCartById", query = "SELECT e FROM Cart e WHERE e.id = :id"),
    @NamedQuery(name="findCartByStatus", query="SELECT e from Cart e where e.status = :status"),
})
public class Cart implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
//    @NotNull
    @Column(name = "ID", nullable = false)
//    @GeneratedValue(strategy=GenerationType.TABLE, generator="CART")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    public enum Status {
        NotYetOrdered, Ordered, Abandonded
    }
    @Column
    private Status status; 
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cartid", orphanRemoval = true)
    private Collection<CartItem> cartItemCollection;
   
    @JoinColumn(name = "CUSTOMERID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(cascade = CascadeType.ALL, optional = false)
    private Customers customerid;
    
    public Cart() { 
      
    }
    
    public Cart(Long id) { 
        this.id = id; 
    }
    
    public Cart(Long id, Customers customerid, Status status) { 
        this.id = id; 
        this.customerid = customerid; 
        this.status = status; 
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public Customers getCustomerid() {
        return customerid;
    }

    public void setCustomerid(Customers customerid) {
        this.customerid = customerid;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Collection<CartItem> getCartItemCollection() {
        return cartItemCollection;
    }

    public void setCartItemCollection(Collection<CartItem> cartItemCollection) {
        this.cartItemCollection = cartItemCollection;
    }
       
    public void addCartItem(CartItem item) {
        cartItemCollection.add(item);  
    }
    
    public void deleteCartItem(CartItem item) {
        cartItemCollection.remove(item);
    }
        
    public int getSize() { 
        return cartItemCollection.size();
    }
   
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cart)) {
            return false;
        }
        Cart other = (Cart) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Cart{" + "id=" + id + ", customerid=" + customerid + ", status=" + status + '}';
    }

   
}